import string, os
import vcs
from vcs import TEST

#TEST	    = 1

TRUE	    = 1
FALSE	    = 0
NL	    = '\n'

TR_TABLE    = string.maketrans(	'ABCDEFGHIJKLMNOPQRSTUVWXYZ ',
								'abcdefghijklmnopqrstuvwxyz_' )


class Revision:
	def __init__(self, rev):
		self.rev = rev
		self.locked_by = ''
		self.date = ''
		self.author = ''
		self.state = ''
		self.lines = ''
		self.branches = ''
		self.log_message = ''
	def __str__(self):
		str = 'revision ' + self.rev
		if self.locked_by:
			str = str + '\tlocked by: ' + self.locked_by
		str = str + '\ndate: %s  author: %s' % (self.date, self.author)
		if self.state:
			str = str + '  state: %s' % self.state
		if self.lines:
			str = str + '  lines: %s' % self.lines
		if self.branches:
			str = str + '\nbranches: '+self.branches
		str = str + NL + self.log_message
		return str
	__repr__ = __str__


class Log:
	REV_SEP     = '-'
	REV_SEPn    = REV_SEP + NL
	END_OF_LOG  = '='
	END_OF_LOGn = END_OF_LOG + NL
	SKIP_TAGS   = { }
	CMD		= 'echo "%s"'
 
	def __init__(self, file, workFile, opt=''):
		self.reused = FALSE
		self.valid = FALSE
		self.workFiles = string.split(workFile, ' ')
		if TEST: print self.workFiles
		self.workFile = ''
		self.count = 0
		cmd = self.CMD % opt
		self.open(cmd, file)
		self.next()
 
	def open(self, cmd, file):
		self.log = vcs.popen(cmd, file)
		self.closed=FALSE
 
	def close(self):
		rc = self.log.close()
		self.closed=TRUE
		return rc
 
	def next(self):
		if self.reused or self.closed:
			self.reused = FALSE
			self.valid = FALSE
			return
		self.parse_log(self.log)
		if not self.valid:
			self.close()
			return
		self.workFile = self.workFiles[self.count]
		self.count=self.count+1
 
	def __nonzero__(self):
		return self.valid or self.reused
 
	def parse_log(self, log):
		self.valid = FALSE
		in_tag = ''
		self.history = []
		self.revisions = []
		line = log.readline()
		if not line:
			self.closed=TRUE
			return
		while line and not string.lstrip(line):
			line = log.readline()
		if not line:
			self.closed=TRUE
			return
		if line == self.END_OF_LOG:
			return
		while line and line != self.END_OF_LOGn:
#	    if TEST: print line,
			if line[0] in ' \t\n':
				line = log.readline()
			else:
				i = string.find(line,':')
				if i < 0:
					line = log.readline()
				else:
					tag = line[:i]
					value = line[i+1:]
					in_tag = string.translate(tag, TR_TABLE)
#		    if TEST: print 'New tag:',in_tag
					line = log.readline()
					if in_tag == 'description':
						while line and not line in (self.REV_SEPn, self.END_OF_LOGn):
							value = value + line
							line = log.readline()
						self.description = string.strip(value)
#			if TEST: print 'Value:',value
						break
					elif in_tag and not self.SKIP_TAGS.has_key(in_tag):
						while line and line[0] in ' \t':
							value = value + string.lstrip(line)
							line = log.readline()
#			if TEST: print 'Value:',value
						setattr(self, in_tag, string.strip(value))
						in_tag = ''
		while line and line != self.END_OF_LOGn:
			self.history.append(string.rstrip(line))
			line = log.readline()
		if not line:
			self.closed=TRUE
		self.valid = TRUE
 
	def reuse(self):
		self.reused = TRUE
 
	def history_str(self):
		return string.join(self.history, NL)
 
	def parse_history(self):
		pass		# override!
 
	def revision(self, revision):
		if not self.revisions:
			self.parse_history()
		for rev in self.revisions:
			if revision == rev.rev: 
				return rev
		return None
 
	def revision_list(self):
		if not self.revisions:
			self.parse_history()
		return self.revisions
 
	def _work_file(self):
		return self.workFile
 
	def work_file(self):
		for dirfile in self.workFiles[self.count-1:]:
			if os.path.basename(dirfile) == self._work_file():
				return dirfile

	def lockers(self):
		return ""

	def _lockers_list(self):
		return []

	def lockers_list(self):
		"Return list of lockers. E.g. [ ['me','1.1'], ['you','1.2'] ]"
		locks = []
		for lock in self._lockers_list():
			locklist = string.split(lock, ':')
			locklist = map(string.strip, locklist)
			locks.append(locklist)
		return locks
